package com.geekaca.db;

import com.geekaca.db.mapper.BrandMapper;
import com.geekaca.db.mapper.OrderMapper;
import com.geekaca.db.pojo.Brand;
import com.geekaca.db.pojo.Order;
import com.geekaca.db.pojo.OrderGood;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMain {
    public static void main(String[] args) {
//        testInsert();
//        selectByConditon(3);
//        testSearch();
//        testWhen();
        //主键返回
        testInsertGen();
        // 根据用户发送的值,来动态的决定更新哪些字段
//        testUpdateDynamic();
//        testDeleteIds();
    }

    private  static   void testDeleteIds(){
        SqlSession sqlSession = openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        int[] ids = {27, 28, 29};
        int deleted = orderMapper.deleteByIds(ids);
        System.out.println("删除了: "+deleted);
    }

    private  static  void testUpdateDynamic(){
        SqlSession sqlSession = openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        Order order = new Order();
        order.setId(10);
        order.setPayment(1999.0);
        order.setPaymentType(2);
        orderMapper.updateOrderDynamic(order);
    }

    private static void testWhen(){
        SqlSession sqlSession = openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = new Brand();
        brand.setBrandName("米");

        brand.setCompanyName("小");
        brand.setStatus(0);

        List<Brand> brands = brandMapper.selectByOneCondition(brand);

        brands.forEach(brd -> System.out.println(brd));

    }


    private static void testSearch() {
        SqlSession sqlSession = openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = new Brand();
//        brand.setBrandName("米");
        brand.setBrandName("");


        // 再加条件
//        brand.setCompanyName("");
        brand.setCompanyName("小");
        List<Brand> brands = brandMapper.selectByConditionDynamic(brand);
        System.out.println("品牌名字含有米的: ");

        brands.forEach(brd -> System.out.println(brd));
    }

    private static void test() {
        /**
         * 获取Mybatis Sessions
         * 获取mapper
         */
        SqlSession sqlSession = openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brandList = brandMapper.selectAllBrands();
        brandList.forEach(brd -> System.out.println(brd));
        System.out.println("===========我是分割线===========");

        Brand brand = brandMapper.selectBId(2);
        if (brand != null) {
            System.out.println("查找到数据: " + brand);
        }
        sqlSession.close();
    }

    private static void testInsert() {
        SqlSession sqlSession = openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Brand brand = new Brand(null, "大米", "大米科技", 0, "大米科技手机", 1);
        int inserted = brandMapper.insertBrand(brand);
        System.out.println("热行insert后,受到影响的记录条数: " + inserted);
        sqlSession.close();
    }

    private static SqlSession openSession() {
        //1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream ips = null;
        try {
            ips = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);
        //2. 获取SqlSession 对象, 用农来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        return sqlSession;
    }

    private static void selectByConditon(Integer id) {
        SqlSession sqlSession = openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = brandMapper.selectByCondition(id);
        if (brands != null) {
            brands.forEach(brd -> System.out.println(brd));
        }
    }
    // 主键回填 并　利用回填主键插入数据
    private static void testInsertGen() {
        SqlSession sqlSession = openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);

        Order order = new Order();
        order.setStatus(2);;
        order.setPayment(1060);
        order.setPaymentType(2);
        int inserted = orderMapper.addOrder(order);
        System.out.println("热行insert后,受到影响的记录条数: " + inserted);

        System.out.println("order id: " + order.getId());

        //  用这个orderId 向tb-order_goods　中插入两条数据 todo完成,数据库查询验证无误
        OrderGood orderGood1 = new OrderGood(null, order.getId(), 20, 1);
        OrderGood orderGood2 = new OrderGood(null, order.getId(), 18, 2);

        orderMapper.addTbOrderGoods(orderGood1);
        orderMapper.addTbOrderGoods(orderGood2);



        sqlSession.close();
    }

}
